package br.com.uniban.entity;

import java.io.Serializable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

@NamedQueries({ @NamedQuery(name = "Questoes.numberOfQuestoes", query = "select COUNT(q) from QuestoesAvulsas as q"),
				@NamedQuery(name = "Questoes.pending", query = "select q from QuestoesAvulsas q where q.flAtiva = false and q.id not in (select qp.questao.id from ProvaQuestao qp where qp.prova.flAtiva = false)")
})
@Entity
@Table(name = "tb_questoes")
public class QuestoesAvulsas implements Serializable {


	private static final long serialVersionUID = 1L;
	@Transient
	public static final Character RESP_A = 'A';
	@Transient
	public static final Character RESP_B = 'B';
	@Transient
	public static final Character RESP_C = 'C';
	@Transient
	public static final Character RESP_D = 'D';

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id_questao")
	private Integer id;

	@Column(nullable = false,columnDefinition="text")
	private String pergunta;

	@Column(nullable = false,columnDefinition="text")
	private String alternativa_A;

	@Column(nullable = false,columnDefinition="text")
	private String alternativa_B;

	@Column(nullable = false,columnDefinition="text")
	private String alternativa_C;

	@Column(nullable = false,columnDefinition="text")
	private String alternativa_D;

	@Column(nullable = false, length = 12)
	private Character resposta;
	
	@Column(name="fl_ativa")
	private Boolean flAtiva;
	
	@OneToOne(optional = true, fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST,
			CascadeType.MERGE, CascadeType.REFRESH })
	@JoinColumn(name = "id_usuario", referencedColumnName = "id_usuario")
	private Usuario usuario;

	@OneToOne(optional = false, cascade = { CascadeType.PERSIST,
			CascadeType.MERGE, CascadeType.REFRESH })
	@JoinColumn(name = "id_disciplina", referencedColumnName = "id_disciplina", nullable = false)
	private Disciplina disciplina;

	@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	@JoinTable(name = "tb_questoes_join_pchaves", joinColumns = { @JoinColumn(name = "id_questao", referencedColumnName = "id_questao") }, inverseJoinColumns = { @JoinColumn(name = "id_pchaves", referencedColumnName = "id_pchaves") })
	private List<PalavrasChaves> listaDePalavrasChaves;
	

	public QuestoesAvulsas() {
		super();
	}
	public QuestoesAvulsas(String pergunta, String alternativa_A,
			String alternativa_B, String alternativa_C, String alternativa_D,
			Character resposta, Usuario usuario, Boolean flAtiva,
			Disciplina disciplina, List<PalavrasChaves> listaDePalavrasChaves) {
		super();
		this.pergunta = pergunta;
		this.alternativa_A = alternativa_A;
		this.alternativa_B = alternativa_B;
		this.alternativa_C = alternativa_C;
		this.alternativa_D = alternativa_D;
		this.resposta = resposta;
		this.usuario = usuario;
		this.flAtiva = flAtiva;
		this.disciplina = disciplina;
		this.listaDePalavrasChaves = listaDePalavrasChaves;
	}


	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getPergunta() {
		return pergunta;
	}

	public void setPergunta(String pergunta) {
		this.pergunta = pergunta;
	}

	public String getAlternativa_A() {
		return alternativa_A;
	}

	public void setAlternativa_A(String alternativa_A) {
		this.alternativa_A = alternativa_A;
	}

	public String getAlternativa_B() {
		return alternativa_B;
	}

	public void setAlternativa_B(String alternativa_B) {
		this.alternativa_B = alternativa_B;
	}

	public String getAlternativa_C() {
		return alternativa_C;
	}

	public void setAlternativa_C(String alternativa_C) {
		this.alternativa_C = alternativa_C;
	}

	public String getAlternativa_D() {
		return alternativa_D;
	}

	public void setAlternativa_D(String alternativa_D) {
		this.alternativa_D = alternativa_D;
	}

	public Character getResposta() {
		return resposta;
	}

	public void setResposta(Character resposta) {
		this.resposta = resposta;
	}

	public Disciplina getDisciplina() {
		return disciplina;
	}

	public void setDisciplina(Disciplina disciplina) {
		this.disciplina = disciplina;
	}

	public List<PalavrasChaves> getListaDePalavrasChaves() {
		return listaDePalavrasChaves;
	}

	public void setListaDePalavrasChaves(
			List<PalavrasChaves> listaDePalavrasChaves) {
		this.listaDePalavrasChaves = listaDePalavrasChaves;
	}
	public Boolean getFlAtiva() {
		return flAtiva;
	}

	public void setFlAtiva(Boolean flAtiva) {
		this.flAtiva = flAtiva;
	}

	public Usuario getUsuario() {
		return usuario;
	}
	public void setUsuario(Usuario usuario) {
		this.usuario = usuario;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((alternativa_A == null) ? 0 : alternativa_A.hashCode());
		result = prime * result
				+ ((alternativa_B == null) ? 0 : alternativa_B.hashCode());
		result = prime * result
				+ ((alternativa_C == null) ? 0 : alternativa_C.hashCode());
		result = prime * result
				+ ((alternativa_D == null) ? 0 : alternativa_D.hashCode());
		result = prime * result
				+ ((disciplina == null) ? 0 : disciplina.hashCode());
		result = prime * result + ((flAtiva == null) ? 0 : flAtiva.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime
				* result
				+ ((listaDePalavrasChaves == null) ? 0 : listaDePalavrasChaves
						.hashCode());
		result = prime * result
				+ ((pergunta == null) ? 0 : pergunta.hashCode());
		result = prime * result
				+ ((resposta == null) ? 0 : resposta.hashCode());
		result = prime * result + ((usuario == null) ? 0 : usuario.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		QuestoesAvulsas other = (QuestoesAvulsas) obj;
		if (alternativa_A == null) {
			if (other.alternativa_A != null)
				return false;
		} else if (!alternativa_A.equals(other.alternativa_A))
			return false;
		if (alternativa_B == null) {
			if (other.alternativa_B != null)
				return false;
		} else if (!alternativa_B.equals(other.alternativa_B))
			return false;
		if (alternativa_C == null) {
			if (other.alternativa_C != null)
				return false;
		} else if (!alternativa_C.equals(other.alternativa_C))
			return false;
		if (alternativa_D == null) {
			if (other.alternativa_D != null)
				return false;
		} else if (!alternativa_D.equals(other.alternativa_D))
			return false;
		if (disciplina == null) {
			if (other.disciplina != null)
				return false;
		} else if (!disciplina.equals(other.disciplina))
			return false;
		if (flAtiva == null) {
			if (other.flAtiva != null)
				return false;
		} else if (!flAtiva.equals(other.flAtiva))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (listaDePalavrasChaves == null) {
			if (other.listaDePalavrasChaves != null)
				return false;
		} else if (!listaDePalavrasChaves.equals(other.listaDePalavrasChaves))
			return false;
		if (pergunta == null) {
			if (other.pergunta != null)
				return false;
		} else if (!pergunta.equals(other.pergunta))
			return false;
		if (resposta == null) {
			if (other.resposta != null)
				return false;
		} else if (!resposta.equals(other.resposta))
			return false;
		if (usuario == null) {
			if (other.usuario != null)
				return false;
		} else if (!usuario.equals(other.usuario))
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "QuestoesAvulsas [id=" + id + ", pergunta=" + pergunta
				+ ", alternativa_A=" + alternativa_A + ", alternativa_B="
				+ alternativa_B + ", alternativa_C=" + alternativa_C
				+ ", alternativa_D=" + alternativa_D + ", resposta=" + resposta
				+ ", flAtiva=" + flAtiva + ", usuario=" + usuario
				+ ", disciplina=" + disciplina + ", listaDePalavrasChaves="
				+ listaDePalavrasChaves + "]";
	}
	
}
